package com.itheima.info.manager.system.controller;

import com.itheima.info.manager.system.domain.Student;
import com.itheima.info.manager.system.domain.Teacher;
import com.itheima.info.manager.system.service.TeacherService;

import java.util.Scanner;

/*
老师用户交互类
键盘录入和输出语句

老师的
入口
增
删
改
查
 */
public class TeacherController {
    private TeacherService teacherService= new TeacherService();
    //死循环
    private Scanner sc = new Scanner(System.in);
    //入口
    public void start() {

        //死循环
        loop:
        while (true) {
            //键盘录入
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            //switch
            switch (choice) {
                case "1":
                   addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("退出老师系统,再见");
                    // break loop;
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }

    public void addTeacher(){
        //录入工号
        System.out.println("请输入工号");
        String sid=null;
        while (true){
            sid=sc.next();
            //判断工号是否存在
            boolean exists = teacherService.isExists(sid);
            if(exists){
                System.out.println("已经存在,请重新录入");
            }else {
                break;
            }
        }
        Teacher teacher = luruTeacherInfo(sid);
        //调用service  添加方法
        boolean b = teacherService.addTeacher(teacher);
        if(b){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

    }

    public Teacher luruTeacherInfo(String sid) {
        //录入其他的信息
        System.out.println("录入姓名");
        String name = sc.next();
        System.out.println("录入年龄");
        int age = sc.nextInt();
        System.out.println("录入薪水");
        double salary = sc.nextDouble();
        Teacher teacher = new Teacher();
        teacher.setSid(sid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setSalary(salary);
        return teacher;
    }

    public void deleteTeacher(){
        String sid = lurugonghao();
        teacherService.deleteTeacher(sid);
        System.out.println("删除成功");



    }

    public String lurugonghao() {
        //录入工号
        System.out.println("请输入工号");
        String sid=null;
        while (true){
            sid=sc.next();
            //判断工号是否存在
            boolean exists = teacherService.isExists(sid);
            if(exists){
                break;
            }else {
                System.out.println("不存在,请重新录入");
            }
        }
        return sid;
    }

    public void updateTeacher(){
        String sid = lurugonghao();
        Teacher teacher = luruTeacherInfo(sid);
        teacherService.updateTeacher(sid,teacher);
        System.out.println("修改成功");

    }
    public void findAllTeachers(){
        Teacher[] teachers = teacherService.findAllTeachers();
        if(teachers==null){
            System.out.println("没有数据请先添加");
            return;
        }
        //有  展示数据
        System.out.println("工号\t姓名\t年龄\t工资");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher!=null) {
                System.out.println(teacher.getSid()+"\t"+teacher.getName()+"\t"+teacher.getAge()+"岁\t"+teacher.getSalary());
            }
        }

    }

}
